using System;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 处理屏幕后期的效果
    /// </summary>
    [CustomEditor(typeof(UGUIPostProcessing))]
    public class UGUIPostProcessingeEditor : Editor
    {
        private void OnEnable()
        {
            UGUIPostProcessing c = target as UGUIPostProcessing;
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            UGUIPostProcessing c = target as UGUIPostProcessing;
            if (GUILayout.Button("执行SetValue"))
            {
                c.SetValue();
            }

            bool isChange = false;
            //SerializedProperty p = new SerializedProperty();
            c.effect = (Material)EditorGUILayout.ObjectField("材质", c.effect, typeof(Material), true);

            if (c.valList.Count == 0)
            {
            }
            else
            {
                foreach (var item in c.valList)
                {
                    EditorGUILayout.BeginVertical();
                    item.name = EditorGUILayout.TextField("name", item.name);
                    item.type = (EMaterialProcessData)EditorGUILayout.EnumPopup("type", item.type);
                    if (item.type == EMaterialProcessData.Color)
                    {
                        var temp = EditorGUILayout.ColorField("color", item.color);
                        if (temp != item.color)
                        {
                            item.color = temp;
                            isChange = true;
                        }
                    }

                    if (item.type == EMaterialProcessData.Vector4)
                    {
                        var temp = EditorGUILayout.Vector4Field("Vector4", item.v4);
                        if (temp != item.v4)
                        {
                            item.v4 = temp;
                            isChange = true;
                        }
                    }

                    if (item.type == EMaterialProcessData.Int)
                    {
                        var temp = EditorGUILayout.IntField("Int", item.intValue);
                        if (temp != item.intValue)
                        {
                            item.intValue = temp;
                            isChange = true;
                        }
                    }

                    if (item.type == EMaterialProcessData.Float)
                    {
                        var temp = EditorGUILayout.FloatField("Float", item.f1);
                        if (Math.Abs(temp - item.f1) > 0.1f)
                        {
                            item.f1 = temp;
                            isChange = true;
                        }
                    }

                    EditorGUILayout.EndVertical();
                }

                if (GUILayout.Button("删除属性"))
                {
                    c.valList.RemoveAt(c.valList.Count - 1);
                }
            }

            if (isChange)
            {
                c.SetValue();
            }

            if (GUILayout.Button("添加属性"))
            {
                c.valList.Add(new MaterialProcessData());
            }
        }
    }
}